package com.dm.wallpaper.library.adapters;

import com.dm.wallpaper.library.ResourceTable;
import com.dm.wallpaper.library.items.Category;
import com.nostra13.universalimageloader.core.ImageLoader;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

public class WallpaperDetailsCategoryAdapter extends RecycleItemProvider {

    private List<Category> data;

    private LayoutScatter layoutScatter;


    public WallpaperDetailsCategoryAdapter(Context context, List<Category> data) {
        this.data = data;
        this.layoutScatter = LayoutScatter.getInstance(context);
    }

    @Override
    public int getCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return data != null ? data.get(i) : null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }


    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        ViewHolder viewHolder = null;
        if (component == null) {
            component = layoutScatter.parse(ResourceTable.Layout_wallpaper_details_category_layout, null, false);
            viewHolder = new ViewHolder((ComponentContainer) component);
            component.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) component.getTag();
        }
        Category category = data.get(position);
//        ComponentContainer.LayoutConfig layoutConfig = viewHolder.image.getLayoutConfig();
//        layoutConfig.width = SystemUtils.getDisplayWidthInPx(mContext) / 2;
//        layoutConfig.height = (int) SystemUtils.vp2px(mContext, 100);
//        viewHolder.image.setLayoutConfig(layoutConfig);
        viewHolder.image.setCornerRadius(15);
        ImageLoader.getInstance().displayImage(category.getThumbUrl(), viewHolder.image);
        viewHolder.tvTitle.setText(category.getName());
        return component;
    }

    static class ViewHolder {

        Text tvTitle;
        Image image;

        public ViewHolder(ComponentContainer componentContainer) {
            tvTitle = (Text) componentContainer.findComponentById(ResourceTable.Id_tv_title);
            image = (Image) componentContainer.findComponentById(ResourceTable.Id_iv_image);
        }
    }

}
